package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Controller {
	private View myView;
	private Model myModel;
	
	public Controller(View view, Model model){
		myView = view;
		myModel = model;
		
		setListeners();
		refreshAll();
	}
	
	private void refreshAll() {
		refreshTables();
	}
	
	public void refreshTables() {
		myView.setRFTable(myModel.generateRegulaFalsiTableModel());
		//myView.setSMTable();
	}
	
	private void setListeners(){
		// POLYNOMIAL
		myView.getTxtPolynomial().addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent ke) {
					myModel.setFunction(myView.getTxtPolynomial().getText());
					//myView.setSecantGraphSeries(0, myModel.generatePolynomialGraphXYSeriesCollection());
					//myView.setRegulaFalsiGraphSeries(0, myModel.generatePolynomialGraphXYSeriesCollection());
					refreshAll();
			}
		});
		
		// TOLERANCE
		myView.getTxtTolerance().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				try {
					myModel.setTolerance(Double.parseDouble(myView.getTxtTolerance().getText()));
					refreshAll();
				} catch(Exception e) {
					
				}
			}
		});
		
		// ITERATIONS
		myView.getTxtIterations().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				try {
					myModel.setItMax(Integer.parseInt(myView.getTxtIterations().getText()));
					refreshAll();
				} catch(Exception e) {
					
				}
			}
		});
		
		// START INTERVAL RF
		myView.getTxtStart().addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent ke) {
				try{
					myModel.setLowerbound(Double.parseDouble(myView.getTxtStart().getText()));
					refreshAll();
				} catch(NumberFormatException e) {
				}
			}
		});
		
		// END INTERVAL RF
		myView.getTxtEnd().addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent ke) {
				try{
					myModel.setUpperbound(Double.parseDouble(myView.getTxtEnd().getText()));
					refreshAll();
				}catch(NumberFormatException e){
				}
			}
		});
		
		// SM X1
		myView.getTxtX1().addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent ke) {
				try{
					myModel.setsX0(Double.parseDouble(myView.getTxtX1().getText()));
					refreshAll();
				}catch(NumberFormatException e){
				}
			}
		});
		
		// SM X2		
		myView.getTxtX2().addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent ke) {
				try{
					myModel.setsX1(Double.parseDouble(myView.getTxtX2().getText()));
					refreshAll();
				}catch(NumberFormatException e){
				}
			}
		});
		
		// RF SLIDER
		myView.getRsIterationSlider().addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				int value = myView.getRsIterationSlider().getValue();
				myView.setRsIterationNumber(value);
				//CHOOSE GRAPH
			}
		});
		
		// SM SLIDER
		myView.getsIterationSlider().addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				int value = myView.getsIterationSlider().getValue();
				myView.setsIterationNumber(value);
				refreshAll();
				//CHOOSE GRAPH
			}
		});
	}	
}
